<template>
    <div class="home">
      <el-container>
        <!-- 侧边栏内容 -->
        <!-- 把width设置成aito就可以跟着折叠了 -->
        <el-aside width="auto">
        <div class="logo"></div>
        <!-- default-active 用于设置默认选中的菜单索引 -->
        <!-- collapse属性控制菜单的折叠与展开 -->
        <!-- router属性开启vue-router模式 -->
        <!-- unique-opened是否只保持一个子菜单的展开 -->
         <el-menu
          unique-opened
          background-color="#F9F9F9"
          router
          class="el-menu-admin"
          :collapse="iscollapse"
          @open="handleOpen"
          @close="handleClose">
          <el-submenu :index="item.path" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item :index="child.path" v-for="child in item.children" :key="child.id">
              <i class="el-icon-menu"></i>
              <span slot="title">{{child.authName}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
       <!-- 右边内容 -->
      <el-container>
        <el-header>
          <!-- 菜单按钮（汉堡包）点击事件toggle -->
           <i class="myicon myicon-menu toggle-btn" @click="toggle"></i>
          <div class="system-title">电商后台管理系统</div>
          <div>
            <!-- username来自登录时的请求 login-data里的username -->
            <!-- 把名字渲染到页面上 -->
             <span class="welcome">您好，{{username}}</span>
            <el-button type="warning" round @click="handleLogout">退出</el-button>
          </div>
        </el-header>
       <el-main>
         <!-- 引入animate动画 -->
          <transition
            mode="out-in"
            enter-active-class="animated fadeIn"
            leave-active-class="animated fadeOut"
          >
            <!-- 用router-view挖坑 -->
            <router-view></router-view>
          </transition>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
// import { getUserList } from '@/api'
import { getMenus } from '@/api'
export default {
  // 控制点击菜单按钮（汉堡包）时，:collapse=""里true或者false，折叠或展开，定义一个变量存起来 iscollapse 默认false展开
  data () {
    return {
      // 控制菜单的折叠与展开
      iscollapse: false,
      // 保存localStorage中获取回来的username 用于获取用户名字功能
      username: '',
      menuList: [] // 存储菜单数据
    }
  },
  // 上面的collapse属性控制菜单的折叠与展开
  methods: {
    handleOpen (key, keyPath) {
      console.log(key, keyPath)
    },
    handleClose (key, keyPath) {
      console.log(key, keyPath)
    },
    toggle () {
      // 取反，当它为true把它变成false，当它为false把它变成true
      this.iscollapse = !this.iscollapse
    },
    // 退出登录功能
    handleLogout () {
      // 清除本地的存储
      localStorage.clear()
      // 跳转到登录login页面
      this.$router.push({ name: 'login' })
    }
  },
  mounted () {
    // getUserList({ query: '', pagenum: 1, pagesize: 1 })
    //   .then(res => {
    //     console.log(res)
    //   })
    // 获取localStorage中保存的username
    this.username = localStorage.getItem('username')
    // 获取菜单列表
    getMenus()
      .then(res => {
        // console.log(res)
        this.menuList = res.data
      })
  }
}
</script>
<style lang="scss" scoped>
.home {
  height: 100%;
  .el-menu-admin:not(.el-menu--collapse) {
    width: 200px;
    min-height: 400px;
  }
  .el-container {
    height: 100%;
  }
  .el-aside {
    background-color: #F9F9F9;
  }
  .el-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    // background-color: #409EFF;
    background-color: #ff5c00;
  }
  .logo {
    height:60px;
    background: url(../assets/logo.png);
    background-size: cover;
    background-color: white;
  }
  .toggle-btn {
    padding: 0 15px;
    margin-left: -20px;
    font-size: 36px;
    color: white;
    cursor: pointer;
    line-height: 60px;
    &:hover {
      // background-color: #4292CF;
      background-color: #e6a23c;
    }
  }
  .system-title {
    font-size: 28px;
    color: white;
  }
  .welcome, {
    color: white;
  }
}
</style>
